package com.e9where.canpoint.customrecycler;

import android.content.Context;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.e9where.canpoint.customrecycler.adapter.BaseAdapter;
import com.e9where.canpoint.customrecycler.adapter.BaseViewHold;
import com.e9where.canpoint.customrecycler.recycler.CustomRecycler;
import com.e9where.canpoint.customrecycler.recycler.callback.SlideCallBcak;
import com.e9where.canpoint.customrecycler.recycler.mode.SlideCallMode;
import com.e9where.canpoint.customrecycler.recycler.mode.SlideMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CustomRecycler viewById;
    private int layout_id=R.layout.adapter_one;
    private Adapter adapter;
    private int page=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final View inflate = LayoutInflater.from(this).inflate(R.layout.head_one, null);
        final View inflate1 = LayoutInflater.from(this).inflate(R.layout.head_two, null);
        final View inflate2 = LayoutInflater.from(this).inflate(R.layout.head_two, null);
        viewById = ((CustomRecycler) findViewById(R.id.recycler));
        viewById.with(SlideMode.BOTH, new SlideCallBcak() {
            @Override
            public void onSlideCallBcak(final SlideCallMode slideCallMode) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewById.onRefreshCompleted();

                        viewById.setSlideMode(SlideMode.BOTH);
                        if (slideCallMode==SlideCallMode.PULL_DOWN)
                        {
                            page=0;
//                            viewById.addHead(inflate1);
//                            adapter.clear();
//                            viewById.removeHead(inflate2);
                            initData(page);
                        }
                        else
                        {
                            page++;
                            if (page<=2)
                            {
                                viewById.addFoot(inflate2);
                                initData2(page);
                            }
                            else
                            {

                                viewById.onMore();
                                viewById.setSlideMode(SlideMode.PULL_DOWN);
                            }
                        }

                    }
                }, 2000);
            }
        });

        adapter = new Adapter(layout_id,this);
        viewById.setAdapter(adapter);
        viewById.setLayoutManager(new GridLayoutManager(this,2));


//        viewById.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//        viewById.setLayoutManager(new LinearLayoutManager(this));
        viewById.setSlideMode(SlideMode.BOTH);
        //添加头部
        viewById.addHead(inflate1);
        viewById.addHead(inflate);
//        viewById.addHead(inflate);
//        viewById.removeHead(inflate1);

        initData(0);

    }

    private void initData(int page)
    {
        List<String> list=new ArrayList<>();
        for (int i = 0; i <10; i++) {
            list.add("我是"+(page*10+i));
        }
        adapter.flush(list);
    };

    private void initData2(int page)
    {
        List<String> list=new ArrayList<>();
        for (int i = 0; i <10; i++) {
            list.add("我是"+(page*10+i));
        }
        adapter.addAll(list);
    };




    class Adapter extends BaseAdapter<String>
    {

        public Adapter(int layout_id, Context context) {
            super(layout_id, context);
        }

        @Override
        protected void setData(BaseViewHold hold, int position, String s) {
            hold.fdTextView(R.id.tv).setText(s);
        }
    }


}
